<?php
namespace App\Http\Controllers\Webapi;
use App\Http\Controllers\Controller;
use App\Models\V1\ChapterModel;
use App\Models\V1\MemberModel;
use Illuminate\Http\Request;
use DB;
use App\Models\V1\DrpModel;

class MoneyController extends Controller
{


    /**
     * 获得销售记录
     * */
    public function sales_log(Request $request)
    {

        $userId     = $request->input('userId');
        $drpModel   = new DrpModel();
        $drpInfo    = $drpModel->get_member_info_by_id($userId);


        $salesLog   = $drpModel->get_drp_incomes($drpInfo->MemNumber);

        return apiData()->set_data('salesLog',$salesLog)->send(200,'ok');
    }



    /**
     * 提现
     * */
    public function apply_tixian(Request $request)
    {
        $role       = $request->input('role');
        $money      = $request->input('money',0);
        $money      = (int) $money;
        $userId     = $request->input('userId');
        $chapterId  = $request->input('chapterId');
        $chapter    = new ChapterModel();

        $bankId     = $request->input('bankId');

        if($bankId < 1)
        {
            return apiData()->send(3002,'选择银行，若无请先添加');
        }

        if($role == 'clo')
        {
            $cloInfo    = $chapter->get_chapter_officer_info($userId);
            $totalMoney = $cloInfo->can_draw_money;

        } elseif($role == 'prisident') {

            $prisident  = $chapter->get_prisident_by_userid($userId);
            $totalMoney = $prisident->can_draw_money;
        }


        if($money > $totalMoney   || $money < 1)
        {

            return apiData()->send(2001,'提现最少为1，最多为'.$totalMoney);

        }

        $bankInfo   = DB::table('paycard')->where('PaCaID',$bankId)->first();
        if(!$bankInfo)
        {
            return apiData()->send(3001,'银行卡不存在');
        }

        $bankType   = DB::table('banks')->where('weixin_bank_code',$bankInfo->WeiXinCardNo)->first();

        $drpModel   = new DrpModel();
        $memberInfo = $drpModel->get_member_info_by_id($userId);

        if($role == 'clo')
        {
            $chapter->use_officer_draw_money($userId,$money);

        } elseif($role == 'prisident') {

            $chapter->use_prisident_draw_money($userId,$money);

        }

        $drpModel->add_draw_money_log($memberInfo->MemNumber,$money,$bankType->bank_name,$bankInfo->CardNo,$memberInfo->YourName);

        return apiData()->send(200,'提现已申请');
    }

    /**
     * 提现页面的数据
     * */
    public function tixian_data(Request $request)
    {
        $userId     = $request->input('userId');
        $drpModel   = new DrpModel();
        $memberModel= new MemberModel();
        $memberInfo = $memberModel->get_member_info_by_id($userId);
        $tixianLogs = $drpModel->get_draw_money_log($memberInfo->MemNumber);
        $status      = ['申请中','成功','失败'];
        foreach($tixianLogs as $log)
        {
            $log->States    = $status[$log->States];
        }

        $bankTypes  = DB::table('banks')->get();

        $userBanks  = DB::table('paycard as a')
            ->leftJoin('banks as b','b.weixin_bank_code','=','a.WeiXinCardNo')
            ->select('b.bank_name','a.CardNo','a.YourName','a.PaCaID')
            ->where('a.DeleteMark',1)
            ->where('a.MemNumber',$memberInfo->MemNumber)
            ->get();

        return apiData()
            ->set_data('tixianLogs',$tixianLogs)
            ->set_data('bankTypes',$bankTypes)
            ->set_data('userBanks',$userBanks)
            ->send(200,'ok');
    }

    /**
     * 添加银行
     * */
    public function add_bank(Request $request)
    {
        $bankNo         = $request->input('bankNo');
        $userId         = $request->input('userId');
        $bankUser       = $request->input('bankUser');
        $bankType       = $request->input('bankType');

        $data   = [
            'bankNo'    => [$bankNo,'缺少卡号',2004],
            'userId'    => [$userId,'重新登录',2005],
            'user'      => [$bankUser,'缺少用户',2006],
            'type'      => [$bankType,'缺少类型',2007]
        ];

        $checkresult    = $this->check_params($data);
        if($checkresult->status == false)
        {
            return apiData()->send($checkresult->code,$checkresult->message);
        }
        //检查这个卡号是否已经存在
        $bankInfo = DB::table('paycard')->where('CardNo',$bankNo)->first();
        if($bankInfo)
        {
            return apiData()->send(2002,'该卡号已经存在');
        }

        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_id($userId);

        $bankInfo       = DB::table('banks')->where('weixin_bank_code',$request->input('bankType'))->first();

        $newBank        = [
            'PayType'       => 1,
            'MemNumber'     => $memberInfo->MemNumber,
            'WeiXinCardNo'   => $bankInfo->weixin_bank_code,
            'CardNo'        => $bankNo,
            'BankType'      => 1,
            'YourName'      => $bankUser,
            'IsDefault'     => 0,
            'DeleteMark'    => 1,
            'ReleaseDate'   => date_time(),
            'AddDate'       => date_time()
        ];

        $result = DB::table('paycard')->insert($newBank);
        if($result)
        {
            return apiData()->send(200,'ok');

        } else {

            return apiData()->send(2001,'添加失败');
        }

    }


    /**
     * 删除银行
     * */
    public function delete_bank(Request $request)
    {
        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_id($request->input('userId'));
        $result = DB::table('paycard')
            ->where('MemNumber',$memberInfo->MemNumber)
            ->where('PaCaID',$request->input('bankId'))
            ->delete();

        if($result)
        {
            return apiData()->send(200,'删除成功');
        }else{

            return apiData()->send(2001,'删除失败');
        }
    }
}